package com.eight.cloud.core.utils.quick;

import java.time.*;
import java.time.format.DateTimeFormatter;

/**
 * 时间工具类
 *
 * @ClassName: DateUtil
 * @Author: TXC
 * @Date: 2025-01-10 16:00
 **/
public class DateUtil {
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String DATE_FORMAT_NO_DASH = "yyyyMMdd";
    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * 日期转字符串
     * @param date 日期
     * @param format 格式
     * @return 字符串
     */
    public static String localDateFormat(LocalDate date, String format) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
        return date.format(dateTimeFormatter);
    }

    /**
     * 时间转字符串
     * @param date 时间
     * @param format 格式
     * @return 字符串
     */
    public static String localDateTimeFormat(LocalDateTime date, String format) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
        return date.format(dateTimeFormatter);
    }

    /**
     * 字符串转日期
     * @param date 字符串
     * @param format 格式
     * @return LocalDate
     */
    public static LocalDate localDateParse(String date, String format) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
        return LocalDate.parse(date, dateTimeFormatter);
    }

    /**
     * 字符串转时间
     * @param date 字符串
     * @param format 格式
     * @return LocalDateTime
     */
    public static LocalDateTime localDateTimeParse(String date, String format) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(date, dateTimeFormatter);
    }

    /**
     * 一天的结束时间
     * @return LocalDateTime
     */
    public static LocalDateTime endOfDay() {
        LocalDate today = LocalDate.now();
        // 获取当天结束时间（23:59:59.999999999）
        return LocalDateTime.of(today, LocalTime.MAX);
    }

    /**
     * 一天的开始时间
     * @return LocalDateTime
     */
    public static LocalDateTime startOfDay() {
        LocalDate today = LocalDate.now();
        // 获取当天开始时间（00:00:00）
        return LocalDateTime.of(today, LocalTime.MIN);
    }

    /**
     * 一天的结束时间戳
     * @return endOfDayTimestamp
     */
    public static Long endOfDayTimestamp() {
        return endOfDay().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    /**
     * 获取有效日期 自动适配月份最后一天
     * @param year
     * @param month
     * @param day
     * @return
     */
    public static LocalDate getValidDate(int year, int month, int day) {
        YearMonth yearMonth = YearMonth.of(year, month);
        int lastDayOfMonth = yearMonth.lengthOfMonth();
        if (day > lastDayOfMonth) {
            day = lastDayOfMonth;
        }
        return LocalDate.of(year, month, day);
    }

    /**
     * 加月自动适配最后一天
     * @param date 加上月份的日期
     * @param month 加上的月份
     * @return
     */
    public static LocalDate plusMonthAdaptLastDay(LocalDate date, int month) {
        int dayOfMonth = date.getDayOfMonth();
        YearMonth yearMonth = YearMonth.from(date);
        if (dayOfMonth >= yearMonth.lengthOfMonth()) {
            yearMonth = yearMonth.plusMonths(month);
            return yearMonth.atDay(yearMonth.lengthOfMonth());
        }
        return date.plusMonths(month);
    }

    /**
     * 减月自动适配最后一天
     * @param date 减去月份的日期
     * @param month 减去的月份
     * @return
     */
    public static LocalDate minusMonthAdaptLastDay(LocalDate date, int month) {
        int dayOfMonth = date.getDayOfMonth();
        YearMonth yearMonth = YearMonth.from(date);
        if (dayOfMonth >= yearMonth.lengthOfMonth()) {
            yearMonth = yearMonth.minusMonths(month);
            return yearMonth.atDay(yearMonth.lengthOfMonth());
        }
        return date.minusMonths(month);
    }
}
